using System.Collections.Generic;
using UnityEngine;

namespace Client.Base
{
    public abstract class StateMachine
    {
        private Dictionary<int,IState> stateDic = new Dictionary<int,IState>();
        private IState curState;
        public abstract void Run(object arg = null);
     
        public void AddState<T>() where T : IState, new()
        {
            int hashCode = typeof(T).GetHashCode();
            stateDic.TryAdd(hashCode, new T());
        }
        public void Change<T>() where T : IState
        {
            int hashCode = typeof(T).GetHashCode();
            if (stateDic.TryGetValue(hashCode, out IState next))
            {
                curState?.OnExit();
                curState = next;
                curState.OnEnter();
            }
        }

        public virtual void Release()
        {
            stateDic.Clear();
            curState = null;
            curState = null;
        }
    }
}

